//
//  HangMenuForCover.m
//  KidReading
//
//  Created by telen on 15/4/1.
//  Copyright (c) 2015年 刘赞黄Telen. All rights reserved.
//

#import "HangMenuForCover.h"
#import "ConstDefine.h"

#define __HANG_W (143.f/[DataProcessing shareSingle].ratio_w)
#define __HANG_H (140.f/[DataProcessing shareSingle].ratio_w)
#define __HANG_BTN_W (110.f/[DataProcessing shareSingle].ratio_w)
#define __HANG_BTN_H (75.f/[DataProcessing shareSingle].ratio_w)
#define __HANG_BTN_POD (20.f/[DataProcessing shareSingle].ratio_w)
#define __HANG_H_S (120.f/[DataProcessing shareSingle].ratio_w)
#define __HANG_H_PT (15/ratio_w)

@implementation HangMenuForCover
{
    UIImageView* imgv;
    CGPoint origin_show;
    CGPoint origin_unshow;
    CGRect endFrame;
}

@synthesize frameOrigin_show = origin_show;
@synthesize frameOrigin_unshow = origin_unshow;

-(void)addBorders{
    self.layer.borderColor = [UIColor greenColor].CGColor;
    self.layer.borderWidth = 1.0;
}

+ (HangMenuForCover *)hangMenuForCover
{
    return [[HangMenuForCover alloc] initWithFrame:CGRectMake(__HANG_W, 0, 1, 1)];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor clearColor];
        CGFloat ratio_w = [DataProcessing shareSingle].ratio_w;
        //
        origin_show = CGPointMake(__HANG_W*0.25, -__HANG_H_PT);
        origin_unshow = CGPointMake(-__HANG_W*1.5, -__HANG_H_PT);
//        orgin_show = CGPointMake(500, -20/ratio_w);
//        orgin_unshow = CGPointMake(300, -50/ratio_w);
        imgv = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, __HANG_W, __HANG_H)];
        [self addSubview:imgv];
        //
        UIImage* image = [UIImage imageNamed:@"guide_bookmark.png"];
        image = [image stretchableImageWithLeftCapWidth:0 topCapHeight:image.size.height*6.f/7.f];
        imgv.image = image;
        //
        frame = imgv.frame;
        frame.origin = origin_unshow;
        self.frame = frame;
        endFrame = frame;
        //
        CGFloat y_r = 30.0/ratio_w/__HANG_H;
        self.layer.anchorPoint = CGPointMake(0.5, y_r);
    }
    return self;
}

- (void)clickSelf
{
    [self show];
}

- (void)setMenuViewArr:(NSArray *)views
{
    _menuViewArr = views;
    imgv.userInteractionEnabled = YES;
    if(views){
        [imgv removeAllSubViews];
        CGPoint pt = self.frame.origin;
        CGRect frame = imgv.frame;
        frame.size.height = __HANG_H+(__HANG_BTN_H+__HANG_BTN_POD)*views.count;
        imgv.frame = frame;
        //
        frame.origin = pt;
        self.frame = frame;
        endFrame = frame;
        //
        CGFloat ratio_w = [DataProcessing shareSingle].ratio_w;
        CGFloat y_r = __HANG_H_PT/frame.size.height;
        self.layer.anchorPoint = CGPointMake(0.5, y_r);
        //
        UIButton* btn_self = [UIButton buttonWithType:UIButtonTypeCustom];
        btn_self.frame = imgv.bounds;
        [imgv addSubview:btn_self];
        [btn_self addTarget:self action:@selector(clickSelf) forControlEvents:UIControlEventTouchUpInside];
        //
        for (int i =0; i<views.count; i++) {
            UIView* oneview = [views objectAtIndex:i];
            [imgv addSubview:oneview];
            oneview.center = CGPointMake(__HANG_W*0.5, __HANG_H_S+__HANG_BTN_H*0.35+i*(__HANG_BTN_H+__HANG_BTN_POD));
        }
    }
}

- (void)show
{
    CGRect frame = endFrame;
    frame.origin = origin_show;
    //
    int direction = self.frame.origin.x < frame.origin.x ? 1 : -1;
    
    //是否子视图可交换
    void(^touchStatus)(BOOL can) = ^(BOOL can){
        for (UIView* view in imgv.subviews) {
            if ([view isKindOfClass:[UIButton class]]) {
                UIButton* btn = (UIButton*)view;
                if(can)btn.alpha = 1.0;
                else btn.alpha = 0.66;
            }
        }
    };
    
    //晃动的block
    void(^shaker)(BOOL finished) = ^(BOOL finished){
        [UIView animateKeyframesWithDuration:2.f delay:0.0 options:0 animations:^{
            [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.1 animations:^{
                self.transform = CGAffineTransformMakeRotation(M_PI * -0.04*direction);
            }];
            [UIView addKeyframeWithRelativeStartTime:0.1 relativeDuration:0.2 animations:^{
                self.transform = CGAffineTransformMakeRotation(M_PI * 0.025*direction);
            }];
            [UIView addKeyframeWithRelativeStartTime:0.3 relativeDuration:0.2 animations:^{
                self.transform = CGAffineTransformMakeRotation(M_PI * -0.015*direction);
            }];
            [UIView addKeyframeWithRelativeStartTime:0.5 relativeDuration:0.2 animations:^{
                self.transform = CGAffineTransformMakeRotation(M_PI * 0.008*direction);
            }];
            [UIView addKeyframeWithRelativeStartTime:0.7 relativeDuration:0.2 animations:^{
                self.transform = CGAffineTransformMakeRotation(M_PI * -0.001*direction);
            }];
            [UIView addKeyframeWithRelativeStartTime:0.9 relativeDuration:0.1 animations:^{
                self.transform = CGAffineTransformMakeRotation(M_PI * 0.00005*direction);
            }];
        } completion:^(BOOL finished) {
            self.transform = CGAffineTransformMakeRotation(0);
            touchStatus(YES);
        }];
    };
    //
    touchStatus(NO);
    [UIView animateWithDuration:0.3f animations:^{
        self.frame = frame;
        self.transform = CGAffineTransformMakeRotation(M_PI*0.03*direction);
    } completion:shaker];
    //
}

- (void)unShow
{
    CGRect frame = self.frame;
    frame.origin = origin_unshow;
    //
    int direction = self.frame.origin.x > frame.origin.x ? 1 : -1;
    //
    [UIView animateWithDuration:0.6f animations:^{
        self.frame = frame;
        self.transform = CGAffineTransformMakeRotation(-M_PI*0.06*direction);
    } completion:^(BOOL finished) {
        self.transform = CGAffineTransformMakeRotation(0);
    }];
}

@end
